package com.baohua.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baohua.dao.DictionaryDao;
import com.baohua.dao.MaterielInformationDao;
import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.MaterielInformation;
import com.baohua.service.MaterielInformationService;
import com.baohua.util.BeanUtils;




@Service("materielInfomationService")

public class MaterielInfomationServiceImpl implements MaterielInformationService {
	

	@Override
	public Map getMaterielInfomation(MaterielInformation mInfo,int page, int pagesize) {
		Map map = new HashMap();
		StringBuffer sqlCount = new StringBuffer("select count(*) from MaterielInformation s where 1=1");
		StringBuffer sql = new StringBuffer("from MaterielInformation s where 1=1");
		if(!StringUtils.isEmpty(mInfo.getMaterielNO())) {
			sql.append(" and s.materielNO = '" + mInfo.getMaterielNO() + "'");
			sqlCount.append(" and s.materielNO = '" + mInfo.getMaterielNO() + "'");
		}
		if(!StringUtils.isEmpty(mInfo.getMaterielName())) {
			sql.append(" and s.materielNam = '" + mInfo.getMaterielName() + "'");
			sqlCount.append(" and s.materielNam = '" + mInfo.getMaterielName() + "'");
		}
		
		List<MaterielInformation> list = materielInformationDao.getMaterielInfomation(sql.toString(),(page-1)*pagesize, pagesize);
		long count = materielInformationDao.countMaterielInfomation(sqlCount.toString());
		map.put("Rows", list);
		map.put("Total", count);
		
		return map;
	}

	@Override
	public void addMaterielInfomation(MaterielInformation m) throws BaohuaSystemException {
		MaterielInformation materielInformation = getMaterielInfomationByMaterialNo(m.getMaterielNO());
		if(materielInformation==null) {
		materielInformationDao.addMaterielInfomation(m);
		}else {
			throw new BaohuaSystemException("物料信息已经存在");
		}

	}

	@Override
	public void updateMaterielInfomation(MaterielInformation materielInfomation) throws BaohuaSystemException {
		
		MaterielInformation m = getMaterielInfomationById(materielInfomation.getMaterielId());
		if(m!=null) {
				if(m.getMaterielNO().equals(materielInfomation.getMaterielNO())) {
					if(!BeanUtils.compareObject(m, materielInfomation)) {
						BeanUtils.copyProperty(materielInfomation, m);
						materielInformationDao.updateMaterielInfomation(m);
					}
			    }else {
			    	MaterielInformation ma1 = getMaterielInfomationByMaterialNo(materielInfomation.getMaterielNO());
					if(ma1==null) {
						BeanUtils.copyProperty(materielInfomation, m);
						materielInformationDao.updateMaterielInfomation(m);
					}else {
						throw new BaohuaSystemException("物料编码已经存在");
					}
				}
				
			}else {
				throw new BaohuaSystemException("修改的物料信息不存在");
			}

	}

	@Override
	public void deleteMaterielInfomation(long id) throws BaohuaSystemException {
		MaterielInformation m = getMaterielInfomationById(id);
		if(m!=null) {
			materielInformationDao.deleteMaterielInfomation(m);
		}else {
			throw new BaohuaSystemException("物料信息不存在");
		}

	}

	@Override
	public MaterielInformation getMaterielInfomationById(Long id) {
		return materielInformationDao.getMaterielInfomationById(id);
	}

	@Override
	public MaterielInformation getMaterielInfomationByMaterialNo(
			String materialNo) {
		return materielInformationDao.getMaterielInfomationByMaterialNo(materialNo);
	}

	public List<String> queryDictionatyByPid(long id) {
		List<String> list = dictionaryDao.findNameForParentID(id);
		return list;
	}
	
	private DictionaryDao dictionaryDao;
	private MaterielInformationDao materielInformationDao;

	
	public DictionaryDao getDictionaryDao() {
		return dictionaryDao;
	}

	@Resource(name="dictionaryDao")
	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}

	public MaterielInformationDao getMaterielInformationDao() {
		return materielInformationDao;
	}

	@Resource(name="materielInfomationDao")
	public void setMaterielInformationDao(
			MaterielInformationDao materielInformationDao) {
		this.materielInformationDao = materielInformationDao;
	}

}
